package employee;

import user.DBUtil;
import user.ToolUtil;
import user.User;
import user.UserDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

public class Register extends JFrame {
    public static User currentUser;   //保存当前登录用户
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JComboBox<Integer> roleComboBox;


    private UserDao userDao = new UserDao();
    private DBUtil dbUtil = new DBUtil();
    public Register(){
        setTitle("企业员工管理系统 - 用户登录");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(450,350);
        setLocationRelativeTo(null);
        getContentPane().setLayout(null);


        //标题
        JLabel titleLable = new JLabel("企业员工管理系统");
        titleLable.setFont(new Font("微软雅黑",Font.BOLD,20));
        getContentPane().add(titleLable);

        //用户明组件
        JLabel usernameLabel = new JLabel("用户名：");
        usernameLabel.setFont(new Font("微软雅黑",Font.BOLD,14));
        usernameLabel.setBounds(80,90,80,25);
        getContentPane().add(usernameLabel);

        usernameField = new JTextField();
        usernameField.setFont(new Font("Dialog",Font.BOLD,14));
        usernameField.setBounds(160,90,200,25);
        getContentPane().add(usernameField);

        //密码组件
        JLabel passwordLabel = new JLabel("密码：");
        passwordLabel.setFont(new Font("微软雅黑",Font.BOLD,14));
        passwordLabel.setBounds(80,130,80,25);
        getContentPane().add(passwordLabel);

        passwordField = new JPasswordField();
        passwordField.setFont(new Font("Dialog",Font.PLAIN,14));
        passwordField.setBounds(160,130,200,25);
        getContentPane().add(passwordField);

        //角色组件
        JLabel roleLablel = new JLabel("角色");
        roleLablel.setFont(new Font("微软雅黑",Font.BOLD,14));
        roleLablel.setBounds(80,170,80,25);
        getContentPane().add(roleLablel);

        roleComboBox = new JComboBox<>();
        roleComboBox.addItem(1);   //普通员工
        roleComboBox.addItem(2);   //管理员

        roleComboBox.setFont(new Font("微软雅黑",Font.PLAIN,14));
        roleComboBox.setBounds(160,170,100,30);
        getContentPane().add(roleComboBox);

        //登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.setFont(new Font("微软雅黑",Font.PLAIN,12));
        loginButton.setBounds(140,220,80,30);
        getContentPane().add(loginButton);

        //注册按钮
        JButton reButton = new JButton("注册");
        reButton.setFont(new Font("微软雅黑",Font.PLAIN,14));
        reButton.setBounds(280,220,80,30);
        getContentPane().add(reButton);

        //登录按钮监听器
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                checkLogin();
                dispose();
            }
        });

//        //注册按钮监听器
        reButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Register();  //打开登录页面
            }
        });
        setVisible(true);
    }
    public void checkLogin(){
//        1.获取文本框的信息
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());
        int roleIndex = roleComboBox.getSelectedIndex() + 1; //1.普通员工  2.管理员

        if(ToolUtil.isEmpty(username) || ToolUtil.isEmpty(password)){
            JOptionPane.showMessageDialog(null,"用户名和密码不能为空");
            return;
        }
        User user1 = new User();
        user1.setUsername(username);
        user1.setPassword(password);
        user1.setRole(roleIndex);

        //3.连接数据库
        Connection conn = null;
        try{
           conn = dbUtil.getConn();
           User loginUser = userDao.login(username,password);
           currentUser = loginUser;

           if(loginUser == null){
               JOptionPane.showMessageDialog(null,"用户名或密码错误");

           }else {
               JOptionPane.showMessageDialog(null,"登录成功");
               dispose();
               if (loginUser.getRole() == 1) {
                   dispose();
                   new EmployeeMenuFrm(loginUser);
               }else {
                   dispose();
                   new AdmiMenuFrm();
               }
           }
        }catch (Exception e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(null,"登录异常"+e.getMessage());
        }
    }
    public static void main(String[] args){
        new Register();
    }
}
